package com.xh.web.service.impl;


import com.xh.web.constant.Constants;
import com.xh.web.domain.DColumn;
import com.xh.web.domain.DProject;
import com.xh.web.domain.DTable;
import com.xh.web.domain.DTableIndex;
import com.xh.web.domain.vo.IndexVo;
import com.xh.web.mapper.DColumnMapper;
import com.xh.web.mapper.DProjectMapper;
import com.xh.web.mapper.DTableIndexMapper;
import com.xh.web.mapper.DTableMapper;
import com.xh.web.service.IDTableIndexService;
import com.xh.web.util.JsonUtil;
import com.xh.web.util.velocity.VelocityInitializer;
import com.xh.web.util.velocity.VelocityUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.StringWriter;
import java.util.*;


/**
 * 索引Service业务层处理
 *
 * @author dongxuefei
 * @date 2020-10-16
 */
@Service
public class DTableIndexServiceImpl implements IDTableIndexService
{
    @Autowired
     private DTableIndexMapper dTableIndexMapper;
    @Autowired
    private DTableMapper dTableMapper;
    @Autowired
   private DColumnMapper dColumnMapper;

    @Autowired
    private DProjectMapper dProjectMapper;


    /**
     * 查询索引列表
     *
     * @param dTableIndex
     * @return
     */
    @Override
    public List<DTableIndex> selectDTableIndexList(DTableIndex dTableIndex)
    {
        return dTableIndexMapper.selectDTableIndexList(dTableIndex);
    }

    /**
     * 新增索引
     *
     * @param
     * @return 结果
     */
    @Override

    public int insertDTableIndex(DTableIndex dTableIndex)
    {

       DTable dTable= dTableMapper.selectDTableByCode(dTableIndex.getTableCode(),dTableIndex.getProjectId());
        //SysUser user = SecurityUtils.getLoginUser().getUser();
        dTableIndex.setTableCode(dTable.getTableCode());
        dTableIndex.setTableName(dTable.getTableName());
        dTableIndex.setCreateBy("admin");
        dTableIndex.setCreateDate(new Date());
        dTableIndex.setUpdateDate(new Date());
        dTableIndex.setUpdateBy("admin");
        dTableIndex.setDelFlag("0");
        return dTableIndexMapper.insertDTableIndex(dTableIndex);
    }

    /**
     * 修改索引
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateDTableIndex(DTableIndex dTableIndex)
    {
        dTableIndex.setUpdateDate(new Date());
        dTableIndex.setUpdateBy("admin");
        DTable dTable= dTableMapper.selectDTableByCode(dTableIndex.getTableCode(),dTableIndex.getProjectId());
        DColumn column= dColumnMapper.selectColumnByCode(dTableIndex.getId(),dTable.getId());
        if(column!=null){
            if(dTableIndex.getIndexColumn().contains(column.getColumnCode())){
                column.setIndexName(dTableIndex.getIndexName());
                column.setIndexType(dTableIndex.getIndexType());
                column.setIndexColumn(dTableIndex.getIndexColumn());
            }else {
                column.setIndexName("");
                column.setIndexType("");
                column.setIndexColumn("");
                column.setIndexId("");
            }
            dColumnMapper.updateDColumn(column);
        }
        return dTableIndexMapper.updateDTableIndex(dTableIndex);
    }



    /**
     * 删除索引信息
     *
     * @param id
     * @return 结果
     */
    @Override
    public int deleteDTableIndexById(String id)
    {
        return dTableIndexMapper.deleteDTableIndexById(id);
    }

    /**
     * 预览代码
     *
     * @param
     * @return 预览数据列表
     */
    @Override
    public Map<String, String> previewCode(String indexId) {
        Map<String, String> dataMap = new LinkedHashMap<>();
        // 查询表信息
        DTableIndex dTableIndex= dTableIndexMapper.selectDTableIndexById(indexId);
        DTable dTable=dTableMapper.selectDTableByCode(dTableIndex.getTableCode(),dTableIndex.getProjectId());
        DColumn dColumn=new DColumn();
        dColumn.setTableId(dTable.getId());
        List<DColumn> columns= dColumnMapper.selectDColumnList(dColumn);
        DProject dProject=dProjectMapper.selectDProjectById(dTableIndex.getProjectId());

        VelocityInitializer.initVelocity();
        VelocityContext context = new VelocityContext();
        context.put("dTableIndex",dTableIndex);
        context.put("dTable",dTable);
        context.put("columnList",columns);
        // 获取模板列表
        List<String> templates = VelocityUtils.getTemplateList("index_"+dProject.getDbType());
        for (String template : templates) {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, Constants.UTF8);
            tpl.merge(context, sw);
            dataMap.put(template, sw.toString());
        }
        return dataMap;
    }


    @Override
    public String getTableTree(String projectId) {
        DTableIndex dTableIndex=new DTableIndex();
        dTableIndex.setProjectId(projectId);
        List<Object> objects = new ArrayList<>();
        Map<String, Object> mapJson = new HashMap<>();
        List<DTableIndex> indexList = dTableIndexMapper.selectDTableIndexList(dTableIndex);
        if (indexList!=null) {
            for (DTableIndex index : indexList) {
                IndexVo indexVo = new IndexVo();
                indexVo.setLabel(index.getIndexName());
                indexVo.setId(index.getId());
                indexVo.setType("index");
                objects.add(indexVo);
            }
        }
        mapJson.put("children", objects);
        //最后返回的拼接内容
        String toJSONString = JsonUtil.toJSONString(mapJson);
        return toJSONString;
    }
}
